package hypeerweb;

import java.io.Serializable;

/**
 * The Class HypercubeCapState, a specific state for a node. Specifically,
 * a Node is in this state if it is the Cap Node of a HyperCube<br/>
 * <br/>
 *
 * <b>Domain:</b>
 * 		STATE_ID	: int
 * 		singleton	: NodeState
 * 
 * <b>Invariants:</b>
 * 		singleton contains only representation of HypercubeCapState in the program
 * 
 * @author Ryan Hess
 */
public class HypercubeCapState extends NodeState implements Serializable {

	/**
	 * 
	 */
	private static final long serialVersionUID = -5261679719894094503L;

	/** The state id that each State returns.  3 for cap state. */
	public static final int STATE_ID = 3;
    
	/** The singleton representation for this state. */
    private static NodeState singleton;

    /**
     * Gets the singleton representation of the cap node state.
     * If no singleton has been created, creates one.
     *
     * @pre <i>None</i>
     * @post the singleton representation for the HypercubeCapState
	 * @return An singleton instance of this NodeState
     */
    public static NodeState getSingleton() {
        if(singleton == null) {
            singleton = new HypercubeCapState();
        }
        return singleton;
    }
    
    /**
     * Default constructor.  Private, only called by getSingleton().
     * 
     * @pre <i>None</i>
     * @post A new empty HypercubeCapState is created
     */
    private HypercubeCapState() {}

    /* (non-Javadoc)
     * @see hypeerweb.NodeState#getCapNode(hypeerweb.Node)
     */
	@Override
    public Node getCapNode(Node self) {
        return self;
    }

    /* (non-Javadoc)
     * @see hypeerweb.NodeState#getInsertionNode(hypeerweb.Node)
     */
	@Override
    public Node getInsertionNode(Node self) {
    	if (self.getFold().getWebIdValue() == 0)
    		return self.getFold();
    	else
    		return self.getSmallestChildlessNeighbor();
    }
    
    /* (non-Javadoc)
     * @see hypeerweb.NodeState#getID()
     */
    @SuppressWarnings("static-access")
	@Override
	public int getID() {
        return this.STATE_ID;
    }
    
    /* (non-Javadoc)
     * @see hypeerweb.NodeState#getChildNodeState(hypeerweb.Node)
     */
	@Override
    public NodeState getChildNodeState(Node self)
    {
    	return HypercubeCapState.getSingleton();
    }

    @Override
    public NodeState getParentNodeState(Node self)
    {
    	return HypercubeCapState.getSingleton();
    }
    
    /* (non-Javadoc)
     * @see hypeerweb.NodeState#toString()
     */
	@Override
    public String toString() {
        return "Hypercube Cap State = 3";
    }
}
